<template>
    <div class="add-area">
    <div class="text">
      <el-icon><Tickets /></el-icon><span>数据列表</span>
    </div>
    <el-button @click="addProductAttribute(route.query.cid,route.query.type)">添加</el-button>
  </div>
  <el-table :data="list" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="编号" width="100" />
        <el-table-column prop="name" label="属性名称" />
        <el-table-column  label="商品类型">
            <template #default="{ row }"
            >
                 {{route.query.cname}}
            </template>
        </el-table-column>
        <el-table-column prop="selectType" label="属性是否可选">
        <template #default="{ row }">
            {{ row.selectType == 0 ? '唯一' : (row.selectType == 1 ? '单选' : (row.selectType == 2 ? '复选' : '')) }}
      </template>
        </el-table-column>
        <el-table-column prop="inputType" label="属性值的录入方式" width="150">
        <template #default="{ row }">
            {{ row.inputType == 0 ? '手工录入' : (row.inputType == 1 ? '从列表中选取' : '') }}
      </template>
        </el-table-column>
        <el-table-column prop="inputList" label="可选值列表"></el-table-column>
        <el-table-column prop="sort" label="排序"></el-table-column>
        <el-table-column label="操作" width="200">
      <template #default="{ row }">
        <el-button @click=" router.push(`/pms/updateProductAttr?id=${row.id}`)">编辑</el-button>
          <el-button type="danger" @click="del(row.id)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
    <div class="foot">
    <div class="left">
      <el-select class="m-2" v-model="optionList" placeholder="批量操作">
        <el-option v-for="item in options" ::key="item.value" :value="item.value" :label="item.label" />
      </el-select>
      <el-button type="primary" @click="ConfirmTn">确定</el-button>
    </div>
    <ElConfigProvider :locale="zhCn">
      <div class="page">
        <el-pagination v-model:current-page="queryParams.pageNum" v-model:page-size="queryParams.pageSize" :page-sizes="[5,10,15]"
          background layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="queryProductAttributeList"
          @current-change="queryProductAttributeList" />
      </div>
    </ElConfigProvider>
  </div>
</template>

<script setup lang="ts">
import { Tickets } from "@element-plus/icons-vue";
import {productAttributeListApi ,delProductAttributeApi,getProductAttributeDeleteApi} from "./api";
import { ElConfigProvider } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElMessage,ElMessageBox} from 'element-plus';
import { reactive, ref,watch } from "vue";
import { useRoute,useRouter } from "vue-router";
const router = useRouter();
const route = useRoute()
const cname= ref(route.query.cname)
const queryParams = reactive<any>({
  cid:route.query.cid,
  pageNum: 1,
  pageSize: 5,
  type:route.query.type,
})
const list = ref<IProductAttributeList[]>([]);
const total = ref(0)
const deleteList = ref<number[]>([])
const optionList = ref([])
const options = [
    { value: '0', label: '删除', },
]
// 添加
const addProductAttribute =(cid:any,type:any)=>{
  router.push(`/pms/addProductAttr?cid=${cid}&type=${type}`)
}
const handleSelectionChange = (val: returnReasonCreateModel[]) => {
    val.forEach(item => {
        list.value.find(sur => sur.id === item.id)
        deleteList.value.push(item.id)
    })
}
// 删除指定商品属性信息
const del = (ids: number) => {
  ElMessageBox.confirm('是否要删除该商品属性信息?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    delProductAttributeApi(ids).then((res) => {
      if (res.code === 200) {
        ElMessage.success('删除成功');
        queryProductAttributeList();
      }
    })
  }).catch(() => { })

}
// 批量操作
const ConfirmTn = () => {
  if (optionList.value[0] == 0) {
        if (deleteList.value.length == 0) {
            ElMessage.warning('请选择要操作的订单')
            return;
        }
        ElMessageBox.confirm("是否要进行批量删除操作?", "提示?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: 'warning',
        }).then(() => {
          getProductAttributeDeleteApi(deleteList.value).then(res => {
                if (res.code === 200) {
                    ElMessage.success('删除成功')
                    queryProductAttributeList();
                }
            })
        })
    } else {
        ElMessage.warning('请选择要执行的操作!')
    }
}
// 封装属性列表数据
const queryProductAttributeList = () => {
    productAttributeListApi(queryParams).then((res) => {
    if (res.code === 200) {
      list.value = res.data.list;
      total.value = res.data.total
    }
  });
};
queryProductAttributeList();
</script>

<style lang="less" scoped>
:deep(.align-c),
:deep(.cell) {
  text-align: center !important;
}

.add-area {
  display: flex;
  justify-content: space-between;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 0 4px #ccc;
  border-radius: 10px;
  .text {
    display: flex;
    align-items: center;
    span {
      padding-left: 10px;
    }
  }
}
.foot {
  display: flex;
  justify-content: space-between;
  padding-top: 30px;
}
</style>